package com.myblog.services.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myblog.Utils.RedisCache;
import com.myblog.domain.GlobalResponse;
import com.myblog.domain.entity.LoginUser;
import com.myblog.domain.entity.User;
import com.myblog.domain.entity.enums.GlobalResponseEnum;
import com.myblog.mapper.UserMapper;
import com.myblog.services.LogoutService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
@Service
public class LogoutServiceImpl extends ServiceImpl<UserMapper, User>
        implements LogoutService {

    @Resource
    RedisCache redisCache;
    @Override
    public GlobalResponse logout() {
        //清除redis中的user即可
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        User user = loginUser.getUser();
        Long id = user.getId();

        redisCache.deleteObject("blogLogin:"+id);

        return GlobalResponse.ok(GlobalResponseEnum.SUCCESS_LOGOUT);
    }
}
